package cn.neusoft.controller;

import cn.neusoft.po.Order;
import cn.neusoft.po.OrderDetail;
import cn.neusoft.query.OrderQuery;
import cn.neusoft.service.IOrderDetailService;
import cn.neusoft.service.IOrderService;
import cn.neusoft.vo.ResponseObj;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author hxj
 * @since 2024-01-02
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    IOrderService orderService;

    @PostMapping
    public ResponseObj addOrder(@RequestBody Order order){
        boolean b = orderService.addOrder(order);
        return b?ResponseObj.SUCCESS("下订单成功",order.getId()):ResponseObj.ERROR(13001,"下订单失败");
    }

    @PostMapping("/page")
    public ResponseObj getByPage(@RequestBody OrderQuery query){
        Page<Order> page = orderService.getByPage(query);
        return ResponseObj.SUCCESS(page);
    }

    @GetMapping("/{id}")
    public ResponseObj getById(@PathVariable("id") Long id){
        Order order = orderService.getById(id);
        return ResponseObj.SUCCESS(order);
    }

    @Autowired
    IOrderDetailService orderDetailService;

    @GetMapping("/detail/{ordersn}")
    public ResponseObj getDetailByOrdersn(@PathVariable("ordersn") String ordersn){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ordersn",ordersn);
        List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);
        return ResponseObj.SUCCESS(orderDetails);
    }

}
